package ar.org.cfired.creditos.gui.action;

import ar.org.cfired.creditos.gui.Sesion;
import ar.org.cfired.hibernate.utils.HibernateUtil;
import ar.org.cfired.creditos.parametros.EstadosLog;
import java.math.BigInteger;

import ar.org.cfired.creditos.parametros.GuardarParametrosException;
import org.hibernate.HibernateException;
import org.hibernate.Session;


public class SuspenderAction extends Action {
	public void execute(Sesion sesion) throws GuardarParametrosException {
		Session hibSession = null;
		try {
			hibSession = HibernateUtil.getSessionFactory().getCurrentSession();		
			hibSession.beginTransaction();
			
			EstadosLog estado = new EstadosLog();
				estado.setLogTask(BigInteger.valueOf(Integer.valueOf(sesion.getTareaActiva().getTaskID()).longValue()));
				estado.setLogUsuario(sesion.getUsuario());
				estado.setLogExecutionId((sesion.getTareaActiva().getExecutionID()==null?"a":sesion.getTareaActiva().getExecutionID()));
				estado.setEstId("SU");
			hibSession.save(estado);
			hibSession.getTransaction().commit();
		}
		catch (HibernateException e) {
			if (hibSession.getTransaction() != null)
				if (hibSession.getTransaction().isActive())
					hibSession.getTransaction().rollback();
			e.printStackTrace();
			throw new GuardarParametrosException(); 
		}
		catch (Exception e) {
			if (hibSession.getTransaction() != null)
				if (hibSession.getTransaction().isActive())
					hibSession.getTransaction().rollback();
			e.printStackTrace();
			throw new GuardarParametrosException();
		}
	}
	
	public void execute(String taskID, String usuario, String executionID) throws GuardarParametrosException {
		Session hibSession = null;
		try {
			hibSession = HibernateUtil.getSessionFactory().getCurrentSession();		
			hibSession.beginTransaction();
			
			EstadosLog estado = new EstadosLog();
				estado.setLogTask(BigInteger.valueOf(Integer.valueOf(taskID).longValue()));
				estado.setLogUsuario(usuario);
				estado.setLogExecutionId((executionID.equals("")?"a":executionID));
				estado.setEstId("SU");
			hibSession.save(estado);
			hibSession.getTransaction().commit();
		}
		catch (HibernateException e) {
			if (hibSession.getTransaction() != null)
				if (hibSession.getTransaction().isActive())
					hibSession.getTransaction().rollback();
			e.printStackTrace();
			throw new GuardarParametrosException(); 
		}
		catch (Exception e) {
			if (hibSession.getTransaction() != null)
				if (hibSession.getTransaction().isActive())
					hibSession.getTransaction().rollback();
			e.printStackTrace();
			throw new GuardarParametrosException();
		}
	}
}
